﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using LeoShi.Soft.OpenSinaAPI;
using System.IO;
using System.Xml;
using LTP.Common;
namespace TCS.Web.MembersHome
{
    public partial class followersmanage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                ViewState["_searchkey"] = "";

                BindDropDown();
                BindData();
            }
        }
        void BindDropDown()
        {
            DataSet dsprovinces = new TCS.BLL.Weibo.WeiBo_provinces().GetProvinces();
            PublicDropdownList.BindDropDownListData(dropProvinces, dsprovinces, "Name", "id");
        }
        void BindData()
        {
            string sort = "FollowerId desc";
            if (rbtfanscount.Checked)
            {
                sort = "FollowerFansCount desc";
            }

            DataSet ds = new TCS.BLL.Weibo.WeiBo_followersInfo().GetList(0, "", sort);
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                updatefriends();
            }
            else
            {
                if (Convert.ToString(ViewState["_searchkey"]) != "")
                {
                    ds = new TCS.BLL.Weibo.WeiBo_followersInfo().GetList(0, Convert.ToString(ViewState["_searchkey"]), sort);
                }
                PagedDataSource pds = new PagedDataSource();
                pds.DataSource = ds.Tables[0].DefaultView;
                pds.AllowPaging = true;
                pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
                pds.PageSize = AspNetPager1.PageSize;
                AspNetPager1.RecordCount = ds.Tables[0].DefaultView.Count;
                this.rptFanslist.DataSource = pds;
                this.rptFanslist.DataBind();
                this.ltrRetcount.Text = AspNetPager1.RecordCount.ToString();
            }
        }
        private void updatefriends()
        {
            var httpRequest = HttpRequestFactory.CreateHttpRequest(Method.GET);
            httpRequest.Token = Session["oauth_token"].ToString();
            httpRequest.TokenSecret = Session["oauth_token_secret"].ToString();
            httpRequest.UserRemoteIP = Request.UserHostAddress;
            string url = "http://api.t.sina.com.cn/statuses/friends.xml";
            string ret = httpRequest.Request(url, "user_id=" + Session["WB_USERID"].ToString());
            DataSet dsret = new DataSet();
            dsret = ConvertXMLToDataSet(ret);
            TCS.Model.Weibo.WeiBo_followersInfo model = new TCS.Model.Weibo.WeiBo_followersInfo();
            TCS.BLL.Weibo.WeiBo_followersInfo bll = new TCS.BLL.Weibo.WeiBo_followersInfo();
            if (dsret.Tables[0].Rows.Count > 0)
            {

                for (int i = 0; i < dsret.Tables[0].Rows.Count; i++)
                {
                    model.CityID = int.Parse(dsret.Tables[0].Rows[i]["city"].ToString());
                    model.ProvinceID = int.Parse(dsret.Tables[0].Rows[i]["province"].ToString());
                    model.FollowerFansCount = int.Parse(dsret.Tables[0].Rows[i]["followers_count"].ToString());
                    model.FollowerFriendShipCount = int.Parse(dsret.Tables[0].Rows[i]["friends_count"].ToString());
                    //model.FansId = int.Parse(dsret.Tables[0].Rows[i]["followers_count"].ToString());
                    model.FollowerName = dsret.Tables[0].Rows[i]["screen_name"].ToString();
                    model.FollowerWBCount = int.Parse(dsret.Tables[0].Rows[i]["statuses_count"].ToString());
                    model.Gender = dsret.Tables[0].Rows[i]["gender"].ToString();
                    model.UserID = int.Parse(Session["memberid"].ToString());
                    model.Verified = dsret.Tables[0].Rows[i]["verified"].ToString() == "true" ? true : false;
                    model.Domain = dsret.Tables[0].Rows[i]["domain"].ToString();
                    model.FollowerWBId = dsret.Tables[0].Rows[i]["id"].ToString();
                    model.Location = dsret.Tables[0].Rows[i]["location"].ToString();
                    model.profile_image_url = dsret.Tables[0].Rows[i]["profile_image_url"].ToString();
                    model.Url = dsret.Tables[0].Rows[i]["url"].ToString();
                    model.Description = dsret.Tables[0].Rows[i]["description"].ToString();
                    bll.Add(model);
                }


            }
            BindData();

        }
        public static DataSet ConvertXMLToDataSet(string xmlData)
        {
            StringReader stream = null;
            XmlTextReader reader = null;
            try
            {
                DataSet xmlDS = new DataSet();
                stream = new StringReader(xmlData);
                //从stream装载到XmlTextReader
                reader = new XmlTextReader(stream);
                xmlDS.ReadXml(reader);
                return xmlDS;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
        }

        protected void dropProvinces_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropProvinces.SelectedValue != "")
            {
                DataSet dsprovinces = new TCS.BLL.Weibo.WeiBo_provinces().GetCityByPID(int.Parse(dropProvinces.SelectedValue));
                PublicDropdownList.BindDropDownListData(dropCity, dsprovinces, "Name", "id");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ViewState["_searchkey"] = " 1=1";
            if (dropProvinces.SelectedValue != "")
            {
                ViewState["_searchkey"] += " and ProvinceID=" + dropProvinces.SelectedValue;
            }
            if (dropCity.SelectedValue != "")
            {
                ViewState["_searchkey"] += " and CityID=" + dropCity.SelectedValue;
            }
            if (rbtF.Checked)
            {
                ViewState["_searchkey"] += " and Gender='F'";
            }
            else if (rbtM.Checked)
            {
                ViewState["_searchkey"] += " and Gender='M'";
            }
            else
            {

            }
            BindData();
        }

        protected void lbtnClearSearchKey_Click(object sender, EventArgs e)
        {
            ViewState["_searchkey"] = "";
            BindData();
        }
        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            BindData();
        }
        protected void rbtfanscount_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtfanscount.Checked)
            {
                BindData();
            }
        }

        protected void rbtdefault_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtdefault.Checked)
            {
                BindData();
            }
        }
    }
}

